//
//  SearchResultDisplayViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "SearchResultDisplayViewController.h"
#import "GameViewController.h"
#import "CityViewController.h"
#import "SearchModel.h"
#import "SearchResultTableViewCell.h"
#import "SearchResultElement.h"
@interface SearchResultDisplayViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *userArr;
@property (nonatomic, retain) NSMutableArray *placeArr;
@property (nonatomic, retain) NSMutableArray *GameArr;
@property (nonatomic, retain) NSMutableArray *placeClickArr;

@end

@implementation SearchResultDisplayViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc{
    [_tableView release];
    [_userArr release];
    [_placeArr release];
    [_GameArr release];
    [_placeClickArr release];
    [_url release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x099cc);
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = YES;
    [self handelData];
    [self createTableView];

}

- (void)createTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    [_tableView release];
    
    [_tableView registerClass:[SearchResultTableViewCell class] forCellReuseIdentifier:@"SearchResultTableViewCell"];
    
//    [_tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:@"BaseTableViewCell3"];
   
    [_tableView registerNib:[UINib nibWithNibName:@"HotPlaceTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HotPlaceTableViewCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return _placeArr.count;
    }else if (section == 1){
        return _GameArr.count;
    }else{
        return _userArr.count;
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"相关目的地";
    }else if (section == 1){
        return @"相关游记故事集";
    }else{
        return @"相关买面粉";
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1) {
        
        Elements *model = _GameArr[indexPath.row];
        
        GameViewController *game = [[GameViewController alloc] init];
        NSString *str = [NSString stringWithFormat:@"%@", model.id];
        game.tripId = str;
        
        [self.navigationController pushViewController:game animated:YES];
        [game release];
    }
    
    if (indexPath.section == 0) {
        SearchModel *model = _placeClickArr[indexPath.row];
        
        CityViewController *city = [[CityViewController alloc] init];
        
        
        //拼接出网址字符串
        NSString *type = [NSString stringWithFormat:@"%@", model.type];
        NSString *tripID = [NSString stringWithFormat:@"%@", model.id];
        
        NSString *url = @"http://api.breadtrip.com/destination/place/2/4087/";
        //上边图片的网址
        NSString *url1 = [url stringByReplacingOccurrencesOfString:@"2" withString:type];
        
        NSString *urlResult = [url1 stringByReplacingOccurrencesOfString:@"4087" withString:tripID];
        
        //推荐地点的网址
        NSString *url2 = @"http://api.breadtrip.com/destination/place/2/4087/pois/all/?start=0&count=20&sort=default&shift=false";
        NSString *resultUrl = [url2 stringByReplacingOccurrencesOfString:@"2" withString:type];
        NSString *resultUrlLast = [resultUrl stringByReplacingOccurrencesOfString:@"4087" withString:tripID];
        city.type = type;
        city.spot_id = tripID;
        city.url2 = resultUrlLast;
        city.url = urlResult;
        if (model.name == nil) {
            city.name = model.name_orig;
        }else{
            city.name = model.name;
            
        }
        [self.navigationController pushViewController:city animated:YES];
        
        [city release];

    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        SearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultTableViewCell"];
        PlaceModel *model = _placeArr[indexPath.row];
        cell.imageViewHeader.image = [UIImage imageNamed:@"iconfont-jingdianxiangqingyejingdianjieshao-2.png"];
        cell.lable1PlaveName.text = model.name;
        return cell;
    }else if (indexPath.section == 1){
        HotPlaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotPlaceTableViewCell"];
        SearchResultElement *model = _GameArr[indexPath.row];
        
        cell.modelElement = model;
        
        return cell;
    }else{
        SearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultTableViewCell"];
        
        UserModel *model = _userArr[indexPath.row];
        cell.layer.cornerRadius = 10;
        cell.layer.masksToBounds = YES;
       
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.imageViewHeader sd_setImageWithURL:[NSURL URLWithString:model.avatar_m] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
        cell.lable1PlaveName.text = model.name;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 50;
    }else if (indexPath.section== 1){
        return 181;
    }else{
        return 50;
    }
}
- (void)handelData{
    NSLog(@"aaaa%@", _url);
    [AFNetWorkingHandle GETRequestWithURLString:_url completionWithSuccess:^(id result) {
        NSDictionary *dic = result;
    
        
        NSDictionary *data = dic[@"data"];
        
        NSArray *userArr = data[@"users"];
        self.userArr = [UserModel ModelHandleWithArray:userArr];
       
        NSArray *placeArr = data[@"places"];
        self.placeArr = [PlaceModel ModelHandleWithArray:placeArr];
        
        self.placeClickArr = [SearchModel ModelHandleWithArray:placeArr];
        
        NSArray *gameArr = data[@"trips"];
        
        self.GameArr = [SearchResultElement ModelHandleWithArray:gameArr];
        
        
        [_tableView reloadData];
        
        if (_GameArr.count == 0 && _placeArr.count == 0 && _userArr.count == 0) {
            
            
            
            UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
            view.backgroundColor = [UIColor whiteColor];
            view.tag = 1000;
            [self.view addSubview:view];
            [view release];
            
            UIImageView *image = [[UIImageView alloc] init];
            image.frame = CGRectMake(0, 0, 100, 100);
            image.tag = 2000;
            image.center = view.center;
            image.image = [UIImage imageNamed:@"iconfont-yewuyeicon05.png"];
            
            [view addSubview:image];
            [image release];
            

            
        }
        
        
        
        
        
        
        
    } withFailure:^(id error) {
        NSLog(@"%@", error);
    }];
}

- (void)viewWillDisappear:(BOOL)animated{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
